package com.xxx.controller;

import com.xxx.controller.utils.R;
import com.xxx.pojo.Address;
import com.xxx.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController extends BaseController {
    @Autowired
    private AddressService addressService;

    //新增收货地址功能
    @RequestMapping("/addNewAddress")
    public R addNewAddress(Address address, HttpSession session){
        addressService.addNewAddress(getUidFromSession(session),getUsernameFromSession(session),address);
        return new R(OK,"添加地址成功");
    }

    //显示收货地址功能
    @RequestMapping("/getAddress")
    public R getAddress(HttpSession session){
        List<Address> result = addressService.getByUid(getUidFromSession(session));
        return new R(OK,result);
    }

    //修改默认地址功能  RestFul风格
    @RequestMapping("/{aid}/setDefault")
    public R setDefault(@PathVariable Integer aid, HttpSession session){
        System.out.println("aid==>"+aid+"uid===>"+getUidFromSession(session)+"username===>"+getUsernameFromSession(session));
        addressService.setDefaultAddress(aid,getUidFromSession(session),getUsernameFromSession(session));
        return new R(OK,"设置成功");
    }

    //删除地址功能 假如删除了默认地址 自动将排序显示的地址信息 第一条设为默认
    @RequestMapping("/deleteAddress/{aid}")
    public R deleteAddress(@PathVariable Integer aid,HttpSession session){
        addressService.deleteAddress(aid,getUidFromSession(session));
    return new R(OK,"删除成功");
    }

    //回显单个收货地址数据功能
    @RequestMapping("/getOneAddress")
    public R getOneAddress(Integer aid){
        Address address = addressService.getOneByAid(aid);
        return new R(OK,address);
    }

    //修改地址功能
    @RequestMapping("/updateAddress/{aid}")
    public R updateAddress(@PathVariable Integer aid,Address address,HttpSession session){
        addressService.updateAddress(address,getUidFromSession(session),getUsernameFromSession(session));
        return new R(OK,"修改地址成功");
    }
}
